//
//  TTDianpuShareView.m
//  TaoTuo
//
//  Created by admin on 2019/12/2.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTDianpuShareView.h"

#define btnH 50

@interface TTDianpuShareView ()

@property (nonatomic, strong) NSMutableArray *titles;
@property (nonatomic, copy) void(^selectBlock)(NSInteger index);

@end

@implementation TTDianpuShareView

+ (instancetype)shareViewWithTitles:(NSArray *)titlesArray selectBlock:(void (^)(NSInteger))selectBlock
{
    CGFloat h = (titlesArray.count + 1) * btnH + 20 + Tabbar_Bottom_height;
    return [[self alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, h) titles:titlesArray selectBlock:selectBlock];
}

- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titlesArray selectBlock:(nonnull void (^)(NSInteger))selectBlock
{
    self = [super initWithFrame:frame];
    if (self) {
        
        if (titlesArray.count > 0) {
            
            self.titles = [NSMutableArray arrayWithArray:titlesArray];
            self.selectBlock = selectBlock;
            
            [self setupSubviews];
        }
        
    }
    return self;
}

///点击了按钮
- (void)clickBtnAction:(UIButton *)sender
{
    NSInteger index = [self.titles indexOfObject:sender.titleLabel.text];
    if (self.selectBlock) {
        self.selectBlock(index);
    }
    
    [LLAnimationAlertView dismissView:self];
}

///点击取消
- (void)cancelBtnAction
{
    [LLAnimationAlertView dismissView:self];
}


- (void)setupSubviews
{
    UIView *titleBGView = [[UIView alloc] initWithFrame:CGRectMake(20, 0, self.width - 40, btnH * self.titles.count)];
    titleBGView.backgroundColor = [UIColor whiteColor];
    titleBGView.layer.cornerRadius = 10;
    [self addSubview:titleBGView];
    
    for (int i = 0; i < self.titles.count; i++) {
        NSString *title = self.titles[i];
        UIButton *btn = [UIButton createButtonWithTitle:title titleColor:UIColorHex(333333) titleFont:[UIFont systemFontOfSize:16] backgroundColor:nil target:self action:@selector(clickBtnAction:)];
        btn.frame = CGRectMake(0, btnH * i, titleBGView.width, btnH);
        [titleBGView addSubview:btn];
        
        if (i < self.titles.count - 1) {
            UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, btnH * (i + 1), titleBGView.width, 1)];
            lineView.backgroundColor = kSeparatorColor;
            [titleBGView addSubview:lineView];
        }
        
    }
    
    UIButton *cancelBtn = [UIButton createButtonWithTitle:@"取消" titleColor:[UIColor redColor] titleFont:[UIFont systemFontOfSize:16] backgroundColor:[UIColor whiteColor] target:self action:@selector(cancelBtnAction)];
    cancelBtn.layer.cornerRadius = 10;
    cancelBtn.frame = CGRectMake(titleBGView.left, titleBGView.bottom + 10, titleBGView.width, btnH);
    [self addSubview:cancelBtn];
    
    
}

@end
